package com.frxuexi.park2304.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.paging.PagingData
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.NoticeBeanItem
import com.frxuexi.lib_base.mvi.BaseFragment
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.park2304.R
import com.frxuexi.park2304.databinding.FragmentHomeBinding
import com.frxuexi.park2304.notice.NoticeAdapter
import com.frxuexi.park2304.notice.NoticeIntent
import com.frxuexi.park2304.notice.NoticeViewModel
import com.frxuexi.park2304.notice.OnItemClickListener
import com.frxuexi.park2304.notice.OnItemLongClickListener
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@AndroidEntryPoint
class HomeFragment : BaseFragment<FragmentHomeBinding,NoticeIntent,NoticeViewModel>() {
    override val binding: FragmentHomeBinding by lazy { FragmentHomeBinding.inflate(layoutInflater) }
    override val vm: NoticeViewModel by viewModels<NoticeViewModel>()
    val noticeAdapter by lazy { NoticeAdapter() }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        noticeAdapter.submitData(it.data as PagingData<NoticeBeanItem>)
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        binding.rvNotice.apply {
            adapter=noticeAdapter
            layoutManager=LinearLayoutManager(requireContext())
        }

        noticeAdapter.setOnItemClickListener(object :OnItemClickListener{
            override fun onItemClick(position: Int) {
                ToastUtils.showLong(position.toString())
            }

        })

        noticeAdapter.setOnItemLongClickListener(object :OnItemLongClickListener{
            override fun onItemLongClick(item: NoticeBeanItem): Boolean {
                ToastUtils.showLong(item.toString())
                return true
            }
        })
        vm.sendIntent(NoticeIntent.getNotices(0))
    }

}